﻿
using DABLL;
using DADAL;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;

namespace DataAcquisition
{
    public partial class FormPointAddForm : BaseForm
    {

        #region 属性
        /// <summary>
        /// 设备点位
        /// </summary>
        public DevicePointItem? DevicePointItemService;

        #endregion

        #region 自定义方法
        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            NumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
        }

        /// <summary>
        /// 刷新
        /// </summary>
        public void Fresh()
        {
            DeviceNumberInput.Enabled = true;
            if (DevicePointItemService is not null)
            {
                DeviceNumberInput.Text = DevicePointItemService.Number.ToString();
                DeviceNameInput.Text = DevicePointItemService.Name;
            }
        }

        #endregion

        #region 系统方法

        public FormPointAddForm()
        {
            InitializeComponent();
        }

        private void FormPointAddForm_Load(object sender, EventArgs e)
        {
            Reload();
        }

        private void DeviceNumberInput_Click(object sender, EventArgs e)
        {
            DeviceNumberInput.Enabled = false;
            DevicePointSelectForm frm = new DevicePointSelectForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            FormPointModel model = new FormPointModel();
            try
            {
                model.Number = Convert.ToInt32(NumberInput.Text);
            }
            catch (Exception)
            {
                Reload();
                Error("界面点位编号错误");
                return;
            }

            if (NameInput.Text.IsNullOrEmpty())
            {
                Error("请输入界面点位名称");
                return;
            }
            model.Name = NameInput.Text;

            try
            {
                model.DevicePointNumber = Convert.ToInt32(DeviceNumberInput.Text);
            }
            catch (Exception)
            {
                Error("设备点位编号错误，请重新选择");
                return;
            }
            if (DeviceNameInput.Text.IsNullOrEmpty())
            {
                Error("请选择设备点位");
                return;
            }
            model.DevicePointName = DeviceNameInput.Text;
            if (DevicePointItemService is not null)
            {
                model.PointType = DevicePointItemService.PointType;
            }
            model.State = 1;
            Response response = FormPoint.AddFormPoint(model);
            if (response.Code != 200)
            {
                Error(response.Msg);
                return;
            }
            else
            {
                this.Close();
                if (this.Owner is not null)
                {
                    FormPointManageForm frm = (FormPointManageForm)this.Owner;
                    frm.Success(response.Msg);
                    frm.LoadList();
                }
            }
        }

        #endregion
    }
}
